package com.logos.ruralrevitalization.controller;

import com.google.common.base.Preconditions;
import com.logos.ruralrevitalization.entity.AttractionReviews;
import com.logos.ruralrevitalization.req.AttractionReviewsReq;
import com.logos.ruralrevitalization.resp.AttractionReviewsResp;
import com.logos.ruralrevitalization.result.Result;
import com.logos.ruralrevitalization.service.AttractionReviewsService;
import com.logos.ruralrevitalization.utils.TouristLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 景点评论表(AttractionReviews)表控制层
 *
 * @author rose
 * @since 2025-03-25 21:58:34
 */
@RestController
@RequestMapping("/attractionReviews")
@Slf4j
public class AttractionReviewsController {

    @Resource
    private AttractionReviewsService attractionReviewsService;

    @Resource
    private TouristLoginUtil touristLoginUtil;

    /**
     * 发表评论
     */
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody AttractionReviewsReq attractionReviewsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AttractionReviewsController.insert.attractionReviewsReq:{}", attractionReviewsReq);
            }
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            Preconditions.checkNotNull(attractionReviewsReq.getAttractionId(), "景点id不能为空");
            Preconditions.checkNotNull(attractionReviewsReq.getComment(), "评论内容不能为空");
            Preconditions.checkNotNull(attractionReviewsReq.getRating(), "评分不能为空");
            attractionReviewsReq.setUserId(Integer.valueOf(id));
            Boolean result = attractionReviewsService.insert(attractionReviewsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AttractionReviewsController.insert.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询景点的所有评论
     */
    @GetMapping("/queryByAttractionId/{attractionId}")
    public Result<List<AttractionReviewsResp>> queryByAttractionId(@PathVariable("attractionId") Integer attractionId) {
        try {
            log.info("AttractionReviewsController.queryByAttractionId.attractionId:{}", attractionId);
            Preconditions.checkNotNull(attractionId, "景点id不能为空");
            List<AttractionReviewsResp> attractionReviewsRespList = attractionReviewsService.queryByAttractionId(attractionId);
            return Result.ok(attractionReviewsRespList);
        } catch (Exception e) {
            log.error("AttractionReviewsController.queryByAttractionId.error:{}", e.getMessage(), e);
            return Result.fail("参数错误");
        }
    }

    /**
     * 删除评论
     */
    @PostMapping("/deleteById")
    public Result<Boolean> deleteById(@RequestBody AttractionReviewsReq attractionReviewsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AttractionReviewsController.deleteById.attractionReviewsReq:{}", attractionReviewsReq);
            }
            String id = touristLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            Boolean result = attractionReviewsService.updateById(attractionReviewsReq);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AttractionReviewsController.deleteById.error:{}", e.getMessage(), e);
            return Result.fail("删除评论失败");
        }
    }
}

